{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "daa6e919-65af-4669-a6d3-4dde9bdf8538",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.utils.data import DataLoader\n",
    "import torchvision.datasets as dsets\n",
    "import torchvision.transforms as transforms\n",
    "import numpy as np\n",
    "import operator\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "batch_size=100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b55e709f-6740-462f-85be-95d41361b2e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train_data: torch.Size([60000, 28, 28])\n",
      "train_labels: torch.Size([60000])\n",
      "test_data: torch.Size([10000, 28, 28])\n",
      "test_labels: torch.Size([10000])\n"
     ]
    }
   ],
   "source": [
    "#下载MNIST dataset\n",
    "train_dataset = dsets.MNIST(root='/data/project/python/torch/pymnist',\n",
    "                            train=True,\n",
    "                            transform=None,\n",
    "                            download=True)\n",
    "test_dataset = dsets.MNIST(root='/data/project/python/torch/pymnist',\n",
    "                            train=False,\n",
    "                            transform=None,\n",
    "                            download=True)\n",
    "\n",
    "#加载数据\n",
    "train_loader = torch.utils.data.DataLoader(dataset=train_dataset,\n",
    "                                           batch_size=batch_size,\n",
    "                                           shuffle=True)\n",
    "test_loader = torch.utils.data.DataLoader(dataset=test_dataset,\n",
    "                                           batch_size=batch_size,\n",
    "                                           shuffle=True)\n",
    "\n",
    "print(\"train_data:\", train_dataset.data.size())            #train_data\n",
    "print(\"train_labels:\", train_dataset.targets.size())       #train_labels\n",
    "print(\"test_data:\", test_dataset.data.size())              #test_data\n",
    "print(\"test_labels:\", test_dataset.targets.size())         #test_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e11f4d4e-9f5c-4858-9bb6-0d644e590408",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEU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4fzjktXrxYa9eu1ZYtW1RYWBhz+8SJEzVkyJCY50NjY6P27ds3oJ4PZzsOvdm1a5ck9a/ng/W7IM7F66+/7vx+v6uurnb//Oc/3b333uuysrJcW1ub9Wp96qGHHnK1tbWupaXF/fWvf3WlpaUuJyfHHTp0yHq1pOrs7HQ7d+50O3fudJLcc88953bu3On+/e9/O+ec+8UvfuGysrLc+vXr3e7du93s2bNdYWGh++yzz4w3T6yvOg6dnZ1u2bJlrr6+3rW0tLjNmze7a6+91l1++eXu2LFj1qsnzKJFi1wgEHC1tbXu4MGD0cvRo0ej91m4cKEbOXKk27Jli9uxY4crKSlxJSUlhlsn3tmOQ1NTk/vZz37mduzY4VpaWtz69evd6NGj3dSpU403j5USAXLOuRdffNGNHDnSpaenu8mTJ7tt27ZZr9TnbrvtNpefn+/S09PdpZde6m677TbX1NRkvVbSvffee07SaZd58+Y55069FfuJJ55weXl5zu/3u+nTp7vGxkbbpZPgq47D0aNH3YwZM9wll1zihgwZ4kaNGuUWLFgw4P4nrbd/f0lu1apV0ft89tln7kc/+pG76KKL3LBhw9wtt9ziDh48aLd0EpztOOzbt89NnTrVZWdnO7/f7y677DL38MMPu3A4bLv4l/DrGAAAJvr9a0AAgIGJAAEATBAgAIAJAgQAMEGAAAAmCBAAwAQBAgCYIEAAABMECABgggABAEwQIACACQIEADDxPwVDG1RxUx1zAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(5)\n"
     ]
    }
   ],
   "source": [
    "#显示数据\n",
    "import matplotlib.pyplot as plt\n",
    "digit = train_loader.dataset.data[0]\n",
    "plt.imshow(digit, cmap=plt.cm.binary)\n",
    "plt.show()\n",
    "print(train_loader.dataset.targets[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "945037d1-a13b-451c-8a29-7fbe961546a7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 读取训练集数据\n",
    "X_train = train_loader.dataset.data.numpy()      #train_data\n",
    "#print(X_train.shape)\n",
    "#将X_train集中的没一副图片转换为一维数组\n",
    "X_train = X_train.reshape(X_train.shape[0], 28*28)\n",
    "#print(X_train.shape)\n",
    "y_train = train_loader.dataset.targets.numpy()   #train_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "20479a47-6b7d-4890-a039-26114cd900a3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#读取测试集数据\n",
    "X_test = test_loader.dataset.data.numpy()      #test_data\n",
    "#将X_test集中的没一副图片转换为一维数组\n",
    "X_test = X_test.reshape(X_test.shape[0], 28*28)\n",
    "#print(X_train.shape)\n",
    "y_test = test_loader.dataset.targets.numpy()   #test_labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "22acdeb4-ceff-41af-a67a-fa11add65acb",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#KNN分类函数\n",
    "def kNN_classify(k, dist, X_train, x_train, Y_test):\n",
    "    assert dist == 'E' or dist == 'M', 'dist must be E or M'\n",
    "    num_test = Y_test.shape[0]\n",
    "    labellist = []\n",
    "    '''\n",
    "    使用欧式距离作为距离度量\n",
    "    '''\n",
    "    if(dist == 'E'):\n",
    "        for i in range(num_test):\n",
    "            #实现欧式距离公式\n",
    "            distances = np.sqrt(np.sum(((X_train - np.tile(Y_test[i], (X_train.shape[0], 1)))**2), axis=1))\n",
    "            nearest_k = np.argsort(distances)\n",
    "            topK = nearest_k[:k]\n",
    "            classCount = {}\n",
    "            for i in topK:\n",
    "                classCount[x_train[i]] = classCount.get(x_train[i],0) + 1\n",
    "            sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(0), reverse=True)\n",
    "            labellist.append(sortedClassCount[0][0])\n",
    "    return np.array(labellist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4919fbe6-506a-49bb-af6c-01afa844c650",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got 2644 / 10000 correct => accuracy: 0.264400\n"
     ]
    }
   ],
   "source": [
    "# KNN分类MNIST\n",
    "import numpy as np\n",
    "num_test = y_test.shape[0]\n",
    "#print(X_train.shape)\n",
    "#print(y_train.shape)\n",
    "#print(X_test)\n",
    "y_test_pred = kNN_classify(5, 'E', X_train, y_train, X_test)\n",
    "num_correct = np.sum(y_test_pred == y_test)\n",
    "accuracy = float(num_correct) / num_test\n",
    "print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8f25089f-bf81-4bd0-922f-68dcbd1e2d7f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def getXmean(data):\n",
    "    image = np.reshape(data, (data.shape[0], -1))\n",
    "    #  将该三维数组转换为一个二维数组，其中每行表示一个图像的像素值。data.shape[0]表示要重新形状的数组的第一维度的长度，而-1表示我们想要numpy自动计算该数组的第二维度的长度，以使总元素数量不变。\n",
    "    mean_image = np.mean(image, axis=0)  # 计算每列的平均值\n",
    "    return mean_image\n",
    "\n",
    "\n",
    "def centralized(data, mean_image):\n",
    "    data = data.reshape((data.shape[0], -1))\n",
    "    data = data.astype(np.float64)\n",
    "    data -= mean_image  # 减去图像均值，实现领均值化\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "af9148e6-67ca-4bd2-9717-94d55d718e7c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f5b56447da0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZ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48ePW7fWp06fPh189NFHwUcffRRICp599tngo48+Cv79738HQRAEP//5z4PRo0cHW7duDQ4cOBAsXLgwKCgoCM6cOWPceWx92TqcPn06ePjhh4Pdu3cHdXV1wTvvvBN861vfCq644oqgvb3duvWYuffee4P09PRg586dwbFjxyJbW1tbZJ977rknGDduXPDuu+8Ge/fuDWbPnh3Mnj3bsOvY+6p1qKmpCZ5++ulg7969QV1dXbB169ZgwoQJwZw5c4w7jzYgAigIguCFF14Ixo0bF6SkpASzZs0KqqqqrFuKu9tuuy3Izc0NUlJSgssuuyy47bbbgpqaGuu2+tx7770XSDpvW7p0aRAE556K/fjjjwfZ2dlBOBwO5s2bF1RXV9s23Qe+bB3a2tqC+fPnB2PGjAmGDh0ajB8/Pli+fHnC/Sett69fUrB+/frIPmfOnAnuu+++4JJLLgmGDx8e3HLLLcGxY8fsmu4DX7UOhw8fDubMmRNkZGQE4XA4uPzyy4Mf/ehHQVNTk23jX8DbMQAATPT7x4AAAImJAAIAmCCAAAAmCCAAgAkCCABgggACAJgggAAAJgggAIAJAggAYIIAAgCYIIAAACYIIACAif8D9rhRtHYY68YAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 显示结果\n",
    "mean_image = getXmean(X_train)\n",
    "cdata = centralized(test_loader.dataset.data.numpy(), mean_image)\n",
    "cdata = cdata.reshape(cdata.shape[0], 28, 28)\n",
    "plt.imshow(cdata[0], cmap=plt.cm.binary)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py39_torch_212",
   "language": "python",
   "name": "py39_torch_212"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
